package io.renren.modules.efinance.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.efinance.entity.AccountflowEntity;
import io.renren.modules.efinance.service.AccountflowService;
import io.renren.modules.efinance.vo.AccountFlowEnum;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 
 *
 * @author lhf
 * @email i@lhf.com
 * @date 2022-06-24 17:18:43
 */
@RestController
@RequestMapping("efinance/accountflow")
public class AccountflowController {
    @Autowired
    private AccountflowService accountflowService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("efinance:accountflow:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = accountflowService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{accountflowId}")
    // @RequiresPermissions("efinance:accountflow:info")
    public R info(@PathVariable("accountflowId") Integer accountflowId){
		AccountflowEntity accountflow = accountflowService.getById(accountflowId);

        return R.ok().put("accountflow", accountflow);
    }

    /**
     * 保存,
     * 充值：插入流水表：本账户->本账户（余额增加）
     * 银行卡余额减少
     */
//    @RequestMapping("/recharge")
//    // @RequiresPermissions("efinance:accountflow:save")
//    public R save(@RequestBody AccountflowEntity accountflow){
//        populateRecharge(accountflow);
//		accountflowService.recharge(accountflow);
//
//        return R.ok();
//    }

    private void populateRecharge(AccountflowEntity accountflow) {
        accountflow.setAccountflowStatus(1);
        accountflow.setAccountflowPayeeaccount(ShiroUtils.getUserEntity().getUsername());
        accountflow.setAccountflowPayeename(ShiroUtils.getUserEntity().getUsername());
        accountflow.setAccountflowPayeraccount(ShiroUtils.getUserEntity().getUsername());
        accountflow.setAccountflowPayername(ShiroUtils.getUserEntity().getUsername());
        accountflow.setAccountflowTime(new Date());
        accountflow.setAccountflowType(AccountFlowEnum.recharge.name());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("efinance:accountflow:update")
    public R update(@RequestBody AccountflowEntity accountflow){
		accountflowService.updateById(accountflow);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("efinance:accountflow:delete")
    public R delete(@RequestBody Integer[] accountflowIds){
		accountflowService.removeByIds(Arrays.asList(accountflowIds));

        return R.ok();
    }

}
